﻿package com.wly.ui
{
	import com.wly.adapter.MapAreaAdapter;
	import com.wly.adapter.MapEditerMainAdapter;
	import com.wly.adapter.ToolbarAdapter;
	import com.wly.resource.RemoteResource;
	import com.wly.ui.common.IItemGrid;
	import com.wly.ui.common.PNGEncoder;
	import com.wly.ui.common.SpriteObject;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	public class MapEditerMain extends SpriteObject
	{
		private var _mapArea:MapArea;
		private var _toolbar:ToolBar;
		private var _btnSave:SimpleButton;
		private var _itemSelect:ItemCell;
		private var _imgArea:ImgArea;
		private var _tfMapIndex:TextField;
		private var _btnIndexOK:SimpleButton;		
		private var _btnLoadImg:SimpleButton;
		private var _btnWork:SimpleButton;
		private var _btnSetAll:SimpleButton;
		private var _btnClear:SimpleButton;
		private var _btnSaveRoad:SimpleButton;
		private var _tfRoadInfo:TextField;
		private var _btnLoadMap:SimpleButton;
		
		private var _mapEditerMainAdapter:MapEditerMainAdapter;
		
		public function MapEditerMain()
		{
			super();
			initChilds();
		}
		
		private function initChilds():void
		{
			_tfMapIndex  = getChildByName("tfMapIndex") as TextField;
			_tfMapIndex.restrict = "0-9";
			_btnIndexOK  = getChildByName("btnIndexOK") as SimpleButton;
			_btnLoadImg  = getChildByName("btnLoadImg") as SimpleButton;
			_mapArea      = getChildByName("mcMapArea") as MapArea;
			_imgArea      = getChildByName("mcImg") as ImgArea;
			_imgArea.parentUI = this;
			_toolbar      = getChildByName("mcToolbar") as ToolBar;
			_btnSave      = getChildByName("btnSave") as SimpleButton;
			_btnWork      = getChildByName("btnWork") as SimpleButton;
			_btnSetAll    = getChildByName("btnSetAll") as SimpleButton;
			_btnClear     = getChildByName("btnClear") as SimpleButton;
			_btnSaveRoad  = getChildByName("btnSaveRoad") as SimpleButton;
			_tfRoadInfo   = getChildByName("tfRoad") as TextField;
			_btnLoadMap   = getChildByName("btnLoadMap") as SimpleButton;
			
			_mapEditerMainAdapter = new MapEditerMainAdapter;
			attachAdapter(_mapEditerMainAdapter);
			_mapEditerMainAdapter.update();
		}
		
		protected function get mainAdapter():MapEditerMainAdapter
		{
			return adapter as MapEditerMainAdapter;
		}
		
		public function updateMapAreaAdapter(adapter:MapAreaAdapter):void
		{
			_mapArea.attachAdapter(adapter);
		}
		
		public function updateToolbar(toolbarAdapter:ToolbarAdapter):void
		{
			_toolbar.attachAdapter(toolbarAdapter);
		}
		
		override public function create():void
		{
			super.create();
			
			_btnSave.addEventListener(MouseEvent.CLICK, onSave);
			_btnIndexOK.addEventListener(MouseEvent.CLICK, onSetMapIndex);
			_btnLoadImg.addEventListener(MouseEvent.CLICK, onLoadImg);
			_btnWork.addEventListener(MouseEvent.CLICK, onSetWork);
			_btnSetAll.addEventListener(MouseEvent.CLICK, onSetAll);
			_btnClear.addEventListener(MouseEvent.CLICK, onClear);
			_btnSaveRoad.addEventListener(MouseEvent.CLICK, onSaveRoad);
			_btnLoadMap.addEventListener(MouseEvent.CLICK, onLoadMap);
		}
		
		override public function destory():void
		{
			_btnSave.removeEventListener(MouseEvent.CLICK, onSave);
			_btnIndexOK.removeEventListener(MouseEvent.CLICK, onSetMapIndex);
			_btnLoadImg.removeEventListener(MouseEvent.CLICK, onLoadImg);
			_btnWork.removeEventListener(MouseEvent.CLICK, onSetWork);
			_btnSetAll.removeEventListener(MouseEvent.CLICK, onSetAll);
			_btnClear.removeEventListener(MouseEvent.CLICK, onClear);
			_btnSaveRoad.removeEventListener(MouseEvent.CLICK, onSaveRoad);
			_btnLoadMap.removeEventListener(MouseEvent.CLICK, onLoadMap);
			_mapArea.deattachAdapter();
			super.destory();
		}
		
		public function onSave(e:MouseEvent):void
		{
			var fr:FileReference = new FileReference();
			var bmd:BitmapData  = new BitmapData(_mapArea.width, _mapArea.height);
			bmd.draw(_mapArea);
		//	var bitArr:ByteArray = PNGEncoder.encode(_mapArea.getBmd());
			var bitArr:ByteArray = PNGEncoder.encode(bmd);
			fr.save(bitArr,"map.png");
		}
		
		public function onSetMapIndex(e:MouseEvent):void
		{
			var index:int= int( _tfMapIndex.text);
			MapEditerMainAdapter.MAPINDEX = index;			
		}
		
		public function onLoadImg(e:MouseEvent):void
		{
			var fr:FileReference = new FileReference();
			fr.browse([new FileFilter("pngImg","*.png")]);
			fr.addEventListener(Event.SELECT, onSelectImg);
		}
		
		private function onSelectImg(e:Event):void
		{
			var fr:FileReference = e.target as FileReference;
			if(fr)
			{
				fr.removeEventListener(Event.SELECT, onSelectImg);
				_imgArea.loadImg(fr.name);
			}
		}
		
		private function onSetWork(e:MouseEvent):void
		{
			if(mainAdapter)
			{
				mainAdapter.switchWorkStat();
			}
		}
		
		private function onSetAll(e:MouseEvent):void
		{
			_mapArea.setAllCell();
		}
		
		private function onClear(e:MouseEvent):void
		{
			_mapArea.clearAllCell();
		}
		
		private function onSaveRoad(e:MouseEvent):void
		{
			//_mapArea.onSaveRoad();
			mainAdapter.setRoadInfo();
		}
		
		private function onLoadMap(e:MouseEvent):void
		{
			var fr:FileReference = new FileReference();
			fr.browse([new FileFilter("pngImg","*.png")]);
			fr.addEventListener(Event.SELECT, onSelectMap);
		}
		
		private function onSelectMap(e:Event):void
		{
			var fr:FileReference = e.target as FileReference;
			if(fr)
			{
				fr.removeEventListener(Event.SELECT, onSelectImg);
				RemoteResource.getInstance().requestRs(fr.name, onUpdateMap);
			}
		}
		
		private function onUpdateMap(data:*):void
		{
			var map:Bitmap = data as Bitmap;
			if(map)
			{
				_mapArea.updateMap(map.bitmapData);
			}
		}
		
		public function set roadInfo(str:String):void
		{
			if(str)
			{
				_tfRoadInfo.text = str;
			}
			else
			{
				_tfRoadInfo.text = "";
			}
		}
	}
}